﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;
using System.Threading;

namespace eipch
{
    /// <summary>
    /// Interaktionslogik für MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private GlobalSettings _MySettings;
        private DataSet _ds;

        public MainWindow()
        {
            InitializeComponent();
            //Wait for displaying splashscreen
            Thread.Sleep(new TimeSpan(0, 0, 1));
            this.init();
        }

        private void init()
        {
            // Setting up window
            this.Title = "Easy IP Changer";
            this.ResizeMode = System.Windows.ResizeMode.NoResize;
            this.WindowStyle = System.Windows.WindowStyle.SingleBorderWindow;
            Application.Current.ShutdownMode = ShutdownMode.OnExplicitShutdown;
            menu_Save.IsEnabled = false;

            // Get global setting
            _MySettings = new GlobalSettings();
            _ds = new DataSet();

            // Get adapter
            List<String> listOfNICs = new NetworkManagement().getListOfNIC();

            foreach (String nic in _MySettings.getNetworkManagement().getListOfNIC())
            {
                try
                {
                    cbox_NICs.Items.Add(nic);
                }
                catch (InvalidOperationException)
                {
                    MessageBox.Show("Fehler beim generieren der Adapterliste", "Fehler");
                }
            }

            cbox_NICs.SelectedIndex = 0;


            // --------------------------------------------------------

            // Get templates
            _ds.Tables.Add(_MySettings.templates().dt);
            cbox_Template.ItemsSource = _MySettings.templates().GetTemplatesList();
            cbox_Template.SelectedIndex = 0;
            // --------------------------------------------------------
        }

        private void init_Settings_Tab()
        {
            //_MySettings.LoadTemplateXML();

            dg_Template.DataContext = _ds.Tables[0];
        }

        private void Exit_Click(object sender, RoutedEventArgs e)
        {
            Application.Current.Shutdown();
        }

        private void Info_Click(object sender, RoutedEventArgs e)
        {

        }

        private void NIC_selection_Changed(object sender, SelectionChangedEventArgs e)
        {
            this.refreshCurrentConfig((e.AddedItems)[0].ToString());
        }

        private void refreshCurrentConfig(String NICName)
        {
            NetworkConfiguration currentConfig = _MySettings.getNetworkManagement().getCurrentConfiguration(NICName);

            if (currentConfig.isSet)
            {
                lbl_currentselectedMAC.Content = currentConfig.mac;
                ckbox_currentselectedDHCP.IsChecked = currentConfig.dhcp;
                lbl_currentselectedIP.Content = currentConfig.ipAddress;
                lbl_currentselectedNM.Content = currentConfig.netMask;
                lbl_currentselectedGW.Content = currentConfig.gateway;
                lbl_currentselectedDNS.Content = "";
                String tmpDNS = "";
                foreach (IPAddress dns in currentConfig.dns)
                    tmpDNS += dns.ToString() + ", ";

                if (tmpDNS.EndsWith(", "))
                    tmpDNS = tmpDNS.Remove(tmpDNS.Length - 2);

                lbl_currentselectedDNS.Content = tmpDNS;

            }

        }

        private void btn_activateSettings_Click(object sender, RoutedEventArgs e)
        {
            //Get template name
            String selectedTemplate = cbox_Template.SelectionBoxItem.ToString();

            NetworkConfiguration newNetworkConfig = _MySettings.templates().getSettings(selectedTemplate);

            _MySettings.getNetworkManagement().setNewConfiguration(cbox_NICs.Text, newNetworkConfig);

            this.refreshCurrentConfig(cbox_NICs.Text);

        }

        private void menu_Save_Click(object sender, RoutedEventArgs e)
        {
            this.save_Config();
        }

        private void save_Config()
        {
            _MySettings.SaveTemplateXML();
        }

        private void Config_Loaded(object sender, RoutedEventArgs e)
        {
            menu_Save.IsEnabled = true;
            this.init_Settings_Tab();
        }

        private void cbox_Template_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            this.refreshCurrentTemplate(e.AddedItems[0].ToString());
        }

        private void refreshCurrentTemplate(string template)
        {

            NetworkConfiguration currentConfig = _MySettings.templates().getSettings(template);
            try
            {
                if (currentConfig.isSet)
                {
                    ckbox_selectedDHCP.IsChecked = currentConfig.dhcp;
                    lbl_selectedIP.Content = currentConfig.ipAddress;
                    lbl_selectedNM.Content = currentConfig.netMask;
                    lbl_selectedGW.Content = currentConfig.gateway;
                    lbl_selectedDNS.Content = "";
                    String tmpDNS = "";
                    foreach (IPAddress dns in currentConfig.dns)
                        tmpDNS += dns + ", ";

                    if (tmpDNS.EndsWith(", "))
                        tmpDNS = tmpDNS.Remove(tmpDNS.Length - 2);
                    lbl_selectedDNS.Content = tmpDNS;
                }
            }
            catch (NullReferenceException)
            {
                MessageBox.Show("Anzeige konnte nicht aktualisiert werden", "Fehler");
                ckbox_selectedDHCP.IsChecked = false;
                lbl_selectedIP.Content = "???";
                lbl_selectedNM.Content = "???";
                lbl_selectedGW.Content = "???";
                lbl_selectedDNS.Content = "???";

            }
        }

        private void menu_DHCPrelease_Click(object sender, RoutedEventArgs e)
        {
            String NIC = cbox_NICs.SelectionBoxItem.ToString();

            _MySettings.getNetworkManagement().DHCP_release(NIC);
            this.refreshCurrentConfig(NIC);
        }

        private void menu_DHCPrenew_Click(object sender, RoutedEventArgs e)
        {
            String NIC = cbox_NICs.SelectionBoxItem.ToString();
            _MySettings.getNetworkManagement().DHCP_renew(NIC);
            this.refreshCurrentConfig(NIC);
        }

        private void Window_Closed_1(object sender, EventArgs e)
        {
            Application.Current.Shutdown();
        }

        private void dg_Template_RowEditEnding(object sender, DataGridRowEditEndingEventArgs e)
        {
            cbox_Template.ItemsSource = _MySettings.templates().GetTemplatesList();
            cbox_Template.SelectedIndex = 0;
        }

        private void dg_Template_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
        {
            cbox_Template.ItemsSource = _MySettings.templates().GetTemplatesList();
            cbox_Template.SelectedIndex = 0;
        }

    }
}
